#region (c) 2014 THN Solutions LLC. - All rights reserved
/*
Simplified BSD License (BSD)
Copyright (c) 2014, THN Solutions LLC. ( www.ThnSolutions.com )
Author: Nguyễn, M. Hải                 ( www.Minh-Hai.com     )
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice, this 
      list of conditions and the following disclaimer.

    * Redistributions in binary form must reproduce the above copyright notice, 
      this list of conditions and the following disclaimer in the documentation 
      and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
POSSIBILITY OF SUCH DAMAGE.
*/
#endregion

#region Using directives
using System;
using System.Text;
using System.Reflection;
using System.Diagnostics;
using System.Windows.Forms;
using System.ComponentModel;

using Thn;
using Thn.Interface.Vcl;
#endregion

namespace Thn.Interface.Vcl
{
#if DEBUG
    [ToolboxItem(false)]
    public
#else
    [ToolboxItem(false)]
    public
#endif
 partial class StackTraceViewer : UserControl
    {
        #region Fields
        StackTrace mCurrentStack = null;
        #endregion

        #region Skip Count
        private int mSkipCount = 0;
        /// <summary>
        /// Gets/Sets SkipCount
        /// </summary>
        [Browsable(true), DefaultValue(0), Category("Behavior"), Description("Filter out a certain number of frames.")]
        public int SkipCount
        {
            get { return mSkipCount; }
            set
            {
                mSkipCount = value;
                if (lstFrames != null) lstFrames.SkipCount = value;
            }
        }
        #endregion

        #region Initialze
        public StackTraceViewer()
        {
            InitializeComponent();
        }

        private void StackTraceViewer_Load(object sender, EventArgs e)
        {

        }
        #endregion

        #region Bind
        /// <summary>
        /// Display a stack trace.
        /// </summary>
        public void Bind(StackTrace stack)
        {
            mCurrentStack = stack;
            lstFrames.Bind(mCurrentStack);
        }
        #endregion

        #region Filter external
        private void chkFilterExternal_ValueChanged(object sender, EventArgs e)
        {
            lstFrames.FilterExternal = chkFilterExternal.Checked;
            lstFrames.Bind(mCurrentStack);
        }
        #endregion

        #region Filter system
        private void chkFilterSystem_ValueChanged(object sender, EventArgs e)
        {
            lstFrames.FilterSystem = chkFilterSystem.Checked;
            lstFrames.Bind(mCurrentStack);
        }
        #endregion

        #region Frame selected
        private void lstFrames_SelectedIndexChanged(object sender, EventArgs e)
        {
            StackFrame frame = lstFrames.SelectedValue as StackFrame;
            if (frame != null)
            {
                int line = frame.GetFileLineNumber();
                int column = frame.GetFileColumnNumber();
                string file = frame.GetFileName();
                MethodBase method = frame.GetMethod();
                Type type = method != null ? method.DeclaringType : null;

                lblFile.Text = string.Format("Line: {0:#,##} Column: {1:#,##} File: {2}", line, column, file);
                lblMethod.Text = method != null ? method.Name : "";
                lblType.Text = type != null ? type.FullName : "";
            }
            else
            {
                lblFile.Text = "";
                lblMethod.Text = "";
                lblType.Text = "";
            }
        }
        #endregion
    }
}
